/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "LinIf.h"
#include "Lin.h"
#include "Mcal.h"
#include "debug.h"

#define PDU_INFO_NUM  0x5u
#define LIN_SEND_DATA_LEN  0x8u

#define LINIF_COMM_INVALID       0xFF
#define LINIF_COMM_SUCCESS       0x00
#define LINIF_COMM_FAIL          0x01
/*PRQA S 1751  EOF*/
uint8 printLog = 0;
uint8 linIfPduIdx = LINIF_COMM_INVALID;
uint8 linIfCommResult = LINIF_COMM_INVALID;

uint8 LinIf_Rcv_Id[LIN_NUM_CHANNELS];

uint8 linslvdataval[PDU_INFO_NUM][LIN_SEND_DATA_LEN] = {
    {0x00, 0x50, 0x90, 0xD0, 0x10, 0xa1, 0x42, 0x85},
    {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
    {0x02, 0x08, 0x36, 0xD0, 0x10, 0xa1, 0x42, 0x85},
    {0x45, 0x39, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};


Lin_PduType linslvPduInfoTable[PDU_INFO_NUM]={
    {
        .Pid= 0x17u,
        .Cs = LIN_ENHANCED_CS,
        .Drc= LIN_MASTER_RESPONSE,
        .Dl = LIN_SEND_DATA_LEN,
        .SduPtr = linslvdataval[0],
    },

    {
        .Pid= 0x18u,
        .Cs = LIN_ENHANCED_CS,
        .Drc= LIN_SLAVE_RESPONSE,
        .Dl = LIN_SEND_DATA_LEN,
        .SduPtr =  linslvdataval[1],
    },
    {
        .Pid= 0x19u,
        .Cs = LIN_ENHANCED_CS,
        .Drc= LIN_MASTER_RESPONSE,
        .Dl = LIN_SEND_DATA_LEN,
        .SduPtr = linslvdataval[2],
    },

    {
        .Pid= 0x1Au,
        .Cs = LIN_ENHANCED_CS,
        .Drc= LIN_SLAVE_RESPONSE,
        .Dl = LIN_SEND_DATA_LEN,
        .SduPtr =  linslvdataval[3],
    },
    {
        .Pid= 0x3Cu,
        .Cs = LIN_ENHANCED_CS,
        .Drc= LIN_MASTER_RESPONSE,
        .Dl = LIN_SEND_DATA_LEN,
        .SduPtr = linslvdataval[2],
    },
};

Std_ReturnType LinIf_HeaderIndication (NetworkHandleType Channel, Lin_PduType* PduPtr)
{
    if (printLog == 1)
    {
        PRINT("Pid 0x%x\n", PduPtr->Pid);
    }
    Std_ReturnType Ret = E_NOT_OK;
    uint32 LinId = PduPtr->Pid & 0x3F;
    LinIf_Rcv_Id[Channel] = LinId;
    for (uint8 i = 0; i < PDU_INFO_NUM; i++) {
        if (LinId == linslvPduInfoTable[i].Pid) {
            *PduPtr = linslvPduInfoTable[i];
            linIfPduIdx = i;
            Ret = E_OK;
            break;
        }
    }
    if (E_OK != Ret)
    {
        linIfCommResult = LINIF_COMM_FAIL;
    }
    return Ret;
}

void LinIf_LinErrorIndication (NetworkHandleType Channel, Lin_SlaveErrorType ErrorStatus)
{
    if (printLog == 1)
    {
        PRINT("chn 0x%x, err 0x%x\n", Channel, ErrorStatus);
    }
    linIfCommResult = LINIF_COMM_FAIL;
}

void LinIf_RxIndication (NetworkHandleType Channel, uint8* Lin_SduPtr)
{
    if (printLog == 1)
    {
	    PRINT("\t chn 0x%x\n", Channel);
    }
    if (linIfPduIdx == LINIF_COMM_INVALID)
    {
        linIfCommResult = LINIF_COMM_FAIL;
    }
    else
    {
        for (uint8 j = 0; j < LIN_SEND_DATA_LEN; j++) {
            if (printLog == 1)
            {
			    PRINT("\t %d\n", Lin_SduPtr[j]);
            }
            if (linslvdataval[linIfPduIdx][j] != Lin_SduPtr[j])
            {
                linIfCommResult = LINIF_COMM_FAIL;
            }
        }
    }
    if (LINIF_COMM_FAIL != linIfCommResult)
    {
        linIfCommResult = LINIF_COMM_SUCCESS;
    }
    /* set lin chn to sleep mode */
    if (LinIf_Rcv_Id[Channel] == LIN_SLEEP_PID && Lin_SduPtr[0] == 0x00U) {
        Lin_GoToSleepInternal(Channel);
    }
}

void LinIf_TxConfirmation (NetworkHandleType Channel)
{
    if (printLog == 1)
    {
        PRINT("\n\t txok 0x%x\n\n", Channel);
    }
}
